var app = getApp()
var util = require("../../utils/util.js")
var orderModel = require("../../model/orderModel.js")
var uploadUtil=require("../../utils/uploadUtil.js")
var settingModel = require('../../model/settingModel.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    evas: ["非常差", "差", "一般", "好", "非常好"],
    tags: [null, null, null, ["整洁"],
      ["干净", "大气", "专业"]
    ],
    arr: [1, 2, 3, 4, 5],
    arrIndex: 4,
    order: null,
    goodsList: null,
    anonymity:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.init()
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  init() {
    var that = this
    util.showLoading("加载中")
    settingModel.getStore(res => {
      wx.hideLoading()
      if (res.status == 1) {
        util.failToast(res.msg)
        return
      }
      that.setData({
        store: res.data
      })
    })
    var order = app.data.tempOrder
    var list = order.list
    order.list = null
    if (order.comment == 0) {
      for (var i = 0; i < list.length; i++) {
        list[i].score = 5
        list[i].imgs = []
      }
    }
    this.setData({
      order: order,
      goodsList: list
    })
  },
  //门店评分
  bindScore(e) {
    var index = e.currentTarget.id
    this.setData({
      arrIndex: index
    })
  },

  //商品评分
  bindGoodsScore(e) {
    var val = e.currentTarget.dataset
    var index = parseInt(val.index)
    var goodsIndex = parseInt(val.goodsindex)
    var list = this.data.goodsList
    list[goodsIndex].score = index + 1
    this.setData({
      goodsList: list
    })
  },
  //商品评论输入监听
  inputEvaluate(e) {
    var goodsIndex = parseInt(e.currentTarget.dataset.goodsindex)
    var list = this.data.goodsList
    list[goodsIndex].content = e.detail.value
  },


  //提交
  bindSubmit() {
    var goodsList = this.data.goodsList
    var order = this.data.order
    var list = []
    for (var i = 0; i < goodsList.length; i++) {
      var goods = goodsList[i]
      var obj = {
        pid: goods.pid,
        pname: goods.pname,
        score: goods.score,
        content: goods.content || "",
        orderId: order.id,
        type: goods.type,
        imgs:goods.imgs,
        anonymity: this.data.anonymity
      }
      list.push(obj)
    }
    var data = {
      evas: JSON.stringify(list)
    }
    util.showLoading("加载中")
    orderModel.evaluate(data, res => {
      wx.hideLoading()
      if (res.status == 1) {
        util.failToast(res.msg)
        return
      }
      util.successToast(res.msg)
      setTimeout(()=>{
          wx.navigateBack()
      },800)
    })
  },
  //添加评价图
  bindAddImg(e) {
    var index = parseInt(e.currentTarget.id)
    var goodsList = this.data.goodsList
    var that=this
    if (goodsList[index].imgs.length>8){
      util.failToast("最多上传9张图片");
      return;
    }
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      success: res => {
        var path = res.tempFiles[0].path
        util.showLoading("加载中")
        uploadUtil.uploadImage(path, url => {
          wx.hideLoading()
          goodsList[index].imgs.push(url)
          that.setData({
            goodsList: goodsList
          })
        })
      }
    })
  },
  //删除评价图
  bindDeleteImg(e){
    var goodsIndex = parseInt(e.currentTarget.dataset.goodsindex)
    var index = parseInt(e.currentTarget.dataset.index)
    var list = this.data.goodsList
    list[goodsIndex].imgs.splice(index,1)
    this.setData({
      goodsList: list,
    })
  },
  bindAnonymity(e){
   this.setData({
     anonymity:e.detail.value.length==0?0:1
   })
  }
})

